import axios from './axios.min.js';
import { getAccessParams } from './common.js';

// 获取 accessToken
async function fetchAccessToken() {
  const data = await getAccessParams();
  const tokenRes = await axios.post('http://10.158.212.196:34002/yxjc/api/yxjc/v1/accessToken', data);
  return tokenRes.data.data.accessToken;
}

// 创建带自动 accessToken 的 axios 实例
export function getAuthAxios() {
  const instance = axios.create ? axios.create() : axios;
  instance.interceptors.request.use(async config => {
    const accessToken = await fetchAccessToken();
    config.headers = config.headers || {};
    config.headers['accessToken'] = accessToken;
    config.headers['XAPP'] = 'YXJC';
    config.headers['Content-Type'] = 'application/json';
    return config;
  });
  return instance;
}
